package com.lovo.project.servlet.product;

import com.lovo.project.enums.ProductEnum;
import com.lovo.project.exception.OurException;
import com.lovo.project.service.ProductService;
import com.lovo.project.service.impl.ProductServiceImpl;
import com.lovo.project.vo.ResultVO;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @title 商品软删除url
 * @author: chenqiang
 * @date: 2020/11/4 14:09
 */
@WebServlet(name = "ProductDeleteServlet",value = "/product/productDelete")
public class ProductDeleteServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            request.setCharacterEncoding("utf-8");
            response.setCharacterEncoding("utf-8");
            response.setContentType("text/html; charset=UTF-8");

            ProductService productService =new ProductServiceImpl();
            Integer productId =Integer.parseInt(request.getParameter("productId"));

            PrintWriter out = response.getWriter();
           try {
            productService.UpdateProductByIsDelete(productId);
            out.print(ResultVO.success().toJson());
           } catch (OurException e) {
            out.print(ResultVO.fail(e.getCodeEnum()).toJson());
           }catch (Exception e1){
            out.print(ResultVO.fail(ProductEnum.PRODUCT_DELETE_ENUM).toJson());
           }finally {
            out.close();
           }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
